package com.audaque.springboot.foshanupload.security.config;

import com.audaque.springboot.foshanupload.authcore.properties.LoginProperties;
import com.audaque.springboot.foshanupload.core.constants.ComConst;
import com.audaque.springboot.foshanupload.web.properties.IgnoreResourcePathProperties;
import com.audaque.springboot.foshanupload.security.interceptor.SecurityContextAuthentication2TransmittableThreadLocalCurrentUserInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;



@Configuration
public class SecurityContextAuthentication2TransmittableThreadLocalCurrentUserInterceptorConfig implements WebMvcConfigurer {
    @Autowired
    public SecurityContextAuthentication2TransmittableThreadLocalCurrentUserInterceptor securityContextAuthentication2TransmittableThreadLocalCurrentUserInterceptor;
    @Autowired
    private LoginProperties loginProperties;
    @Autowired
    private IgnoreResourcePathProperties ignoreResourcePathProperties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //表示要拦截
        String[] addPathPatterns = {
                ComConst.INTERCEPTOR_ANY_MATCH
        };

        //要排除的路径
        List<String> excludePathPatterns = ignoreResourcePathProperties.getList();
        excludePathPatterns.addAll(excludePathPatterns);
        excludePathPatterns.add(loginProperties.getRegisterUrl());
        excludePathPatterns.add(loginProperties.getLoginUrl());
        excludePathPatterns.add(loginProperties.getLogoutUrl());
        String excludedUris = String.join(",", excludePathPatterns);

        registry.addInterceptor(securityContextAuthentication2TransmittableThreadLocalCurrentUserInterceptor)
                .addPathPatterns(addPathPatterns).excludePathPatterns(excludedUris).order(100);
    }


}
